package test.nettys.nio;


import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * @Author wangshaopeng
 * @Date 2021/8/10
 */
public class NioServer {

    public static void main(String[] args) throws IOException {

        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        serverSocketChannel.configureBlocking(false);

        ServerSocket socket = serverSocketChannel.socket();
        socket.bind(new InetSocketAddress(8899));


        Selector selector = Selector.open();
        serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
        
        

        List<SocketChannel> clients = new ArrayList<>();

        while (true) {
            selector.select();
            Set<SelectionKey> selectionKeys = selector.selectedKeys();

            Iterator<SelectionKey> iterator = selectionKeys.iterator();

            while (iterator.hasNext()) {
                SelectionKey selectionKey = iterator.next();
                iterator.remove();
                if (selectionKey.isAcceptable()) {
                    ServerSocketChannel serverChannel = (ServerSocketChannel) selectionKey.channel();
                    SocketChannel client = serverChannel.accept();
                    client.configureBlocking(false);
                    client.register(selector, SelectionKey.OP_READ);
                    clients.add( client);
                }else if(selectionKey.isReadable()){
                    SocketChannel client = (SocketChannel) selectionKey.channel();
                    ByteBuffer buffer = ByteBuffer.allocate(100);
                    if (client.read(buffer)>0) {
                        buffer.flip();

                        Charset charset = Charset.forName("utf-8");
                        String msg = String.valueOf(charset.decode(buffer).array());
                        System.out.println("收到消息"+msg);

                        clients.forEach(c->{
                            ByteBuffer write = ByteBuffer.allocate(100);
                            try {
                                write.put(("c:"+client.getRemoteAddress()+" : "+msg).getBytes());
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            write.flip();
                            try {
                                c.write(write);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }

                        });

                    }

                }





            }



        }


    }
}
